<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    Mirze<mirze@foxmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_mc_transfer_log.class.php 2011-07-07 12:05:41 $
 * @Show Note: 设备转资记录表
 */
Class zy_mc_transfer_log extends TheAdmin
{
	/* DataBase object */
	public $db;

	/* View the data page limit items */
	public $pageSize = PAGENO;
	
	/**
	 *	Construct this module class.
	*/
	public function zy_mc_transfer_log()
	{
	}
	public function set()
	{
		$this->TheAdmin();
		//定义模版
		$this->tplFile ="zy_mc_transfer_log.tpl";
		$this->admin_user_id = $this->userRequest->get('admin_user_id');
		//取得数据库连接
		$this->db = Factory::getDB("zy_mc_transfer_log");
		//取得用户对于该类的权限
		$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
		$class_p_arr = $this->show_p();
		//取得该类的权值数组
		$class_method = $class_p_arr['class_method'];
		//取得用户操作菜单
		$menu = array();
		foreach ($class_method as $k=>$v)
		{
			if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
			{
				array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
			}
		}
		//用户有权菜单生成 可以添加其他操作
		$this->smarty->assign('menus',$menu);
		$this->smarty->assign("main_menus",$this->get_user_menus());
	}
	
	/**
	 *	Call class construct method.
	*/
	public function __construct()
	{
			$this->zy_mc_transfer_log();
	}
	public function _display()
	{
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	default method.
	*/
	public function show()
	{
		$col = "transfer_log_id";
		$where = 1;
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//数据库执行sql语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if( 0 > $rowNo) goToIndex($rowNo);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		$arr = $this->db->getAll($where,$col,$numrows,$offset);
		admin_log($this->admin_user_id,__METHOD__,'查看',1);
		//数据库执行select语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if(0 > $arr ) goToIndex($arr);
		
		$this->smarty->assign('arr',$arr);
		//默认方法
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		$this->smarty->assign('act','list');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	add method.
	*/
	public function add()
	{
		//合同号
		$contract_no = isset($_REQUEST['contract_no']) ? trim($_REQUEST['contract_no']) : '' ;
		//转资ID
		$transfer_id = isset($_REQUEST['transfer_id']) ? trim($_REQUEST['transfer_id']) : '' ;
		if(empty($contract_no)) {
			alertMsg2("合同号为空", 'index.php?c=zy_mc_transfer');
		}

		$db_zafm = Factory::getDB('zy_auth_finish_mc');

		//添加数据
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{			
			$where = 1;
			if($_GET['cn_name']){
				$where = "zml.mc_cn_name like '%{$_GET['cn_name']}%'";
			}
			$data_list = $db_zafm->getRecord($contract_no, 1,'',-1,-1,'*',$where);
			if($_GET['cn_name']){
				if(!count($data_list)){
					alertMsg2('没有该名称设备');
				}
			}
			if(empty($data_list)) {
				alertMsg2("该转资单无验收通过设备", 'index.php?c=zy_mc_transfer');
			}
			$this->smarty->assign('contract_no',$contract_no);
			$this->smarty->assign('transfer_id',$transfer_id);
			
			
			//分页，通过查询所有来去
			$rowNo = count($data_list);
			$page = Factory::getBaseClass('pagination');
			$page->set($rowNo,$this->pageSize);
			$numrows = $this->pageSize;
			$offset  = $page->getSqlMove();
			//unset($zy_plan_reply_mc_arr);
			for ($i=$offset;$i<($numrows+$offset);$i++){
				if(isset($data_list[$i]))
				$page_arr[] = $data_list[$i];
			}
			$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
			$this->smarty->assign('data_list',$page_arr);
		
			//使用模版，展示提交界面
			$this->smarty->assign('act_title','添加');
			$this->smarty->assign('act','add');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			$temp = $this->getValue();
			if(empty($temp['comp_no'])) {
				alertMsg2('请勾选产品名称');
			}

			foreach($temp['comp_no'] as $k => $v) {
				$values = array(
					'comp_no' =>  $v,
					'finance_no' => $temp['finance_no'][$v],
					'transfer_info' => $temp['transfer_info'][$v],
					'mc_log_id' => $temp['mc_log_id'][$v], //设备id
					'transfer_id' => $transfer_id,
					'transfer_no' => 1, //转资数量
					'transfer_time' =>time(),
				);
				$insrt = $this->db->insert_zy_mc_transfer_log($values);
				if($insrt > 0) {//转资成功：更新状态
					$val_zafm = array('mc_state'=>2);
					$where = "comp_no = '{$v}' and contract_no='{$contract_no}'";
					$upId = $db_zafm->update_zy_auth_finish_mc($val_zafm, $where);
				}
			}
			//$insrt = $this->db->insert_zy_mc_transfer_log($temp);
			$error_no = 0 > $insrt ?  $insrt :'-1501';
			admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			//alertMsg($error_no, 'index.php?c=zy_mc_transfer');
			if(0 < $insrt) { //成功直接跳转
				redirectPage("index.php?c=zy_mc_transfer&m=browse&transfer_id={$transfer_id}");
			} else {
				alertMsg($error_no, 'index.php?c=zy_mc_transfer');
			}
		}
	}
	
	/**
	 *	edit method.
	*/
	public function edit()
	{
		//取得id，也就是唯一索引
		$transfer_log_id = isset($_REQUEST['transfer_log_id']) ? intval(trim($_REQUEST['transfer_log_id'])) : 0 ;
		//取得是否查看详情
		$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$result = $this->db->getOne($transfer_log_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_mc_transfer_log');
			}
			$mc_log_db = Factory::getDB('zy_mc_log');
			$temp_arr = $mc_log_db->getOne($result['mc_log_id']);
			$result = array_merge($result,$temp_arr);
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','修改');
			$this->smarty->assign('act','edit');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			//设置where条件
			$where = "transfer_log_id=$transfer_log_id";
			$temp = $this->getValue(1);
			$update = $this->db->update_zy_mc_transfer_log($temp,$where);
			//print_r($this->db->getDebug());exit;
			admin_log($this->admin_user_id,__METHOD__,$where,2);
			$error_no = 0 > $update ?  $update :'-1502';
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			//alertMsg($error_no, 'index.php?c=zy_mc_transfer_log');
			redirectPage("index.php?c=zy_mc_transfer&m=browse&transfer_id={$temp['transfer_id']}");
		}
	}
	
	/**
	 *	browse method.
	*/
	public function browse() {
		$transfer_log_id = isset($_REQUEST['transfer_log_id']) ? intval(trim($_REQUEST['transfer_log_id'])) : 0 ;
		$transfer_id = isset($_REQUEST['transfer_id']) ? intval($_REQUEST['transfer_id']) : 0 ;
		//使用模版，展示提交界面
		$result = $this->db->getOne($transfer_log_id);
		if(empty($result))
		{
			alertMsg('-2001', 'index.php?c=zy_mc_transfer_log');
		}
		foreach ($result AS $k=>$v)
		{
			$this->smarty->assign("{$k}",$v);
		}

		//对应验收记录
		$db_zaml = Factory::getDB('zy_auth_mc_log');
		$recordArr =  $db_zaml->getMcInfo($transfer_id, $transfer_log_id);

		$this->smarty->assign('recordArr',$recordArr[0]);
		//print_r($recordArr);
		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('act_title','查看');
		$this->smarty->assign('act','browse');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	del method.
	*/
	public function del()
	{
		//取得id，也就是唯一索引
		$transfer_log_id = isset($_REQUEST['transfer_log_id']) ? intval(trim($_REQUEST['transfer_log_id'])) : 0 ;
		$where = "transfer_log_id=$transfer_log_id";
		$del=$this->db->delete_zy_mc_transfer_log($where);
		admin_log($this->admin_user_id,__METHOD__,$where,3);
		$error_no = 0 > $del ?  $del :'-1503';
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		alertMsg($error_no, 'index.php?c=zy_mc_transfer_log');
	}
	
	/**
	 *	get request method.
	*/
	public function &getValue($isEdit=0)
	{
		$temp = array();
		$check = Factory::getBaseClass("checkBase");
		if($isEdit){
			$temp['transfer_log_id'] = $_POST['transfer_log_id'];
		} else {
			$temp['transfer_log_id'] = "";
		}
		//补充其他的字段
		$temp['finance_no'] = $_POST['finance_no'];
		$temp['transfer_id'] = $_POST['transfer_id'];
		$temp['transfer_info'] = $_POST['transfer_info'];
		$temp['transfer_no'] = $_POST['transfer_no'];
		$temp['comp_no'] = $_POST['comp_no'];
		$temp['mc_log_id'] = $_POST['mc_log_id'];
				
		admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
		return $temp;
	}
	/**
	 *	get class method and popedom.
	*/
	public function show_p()
	{
		//该方法为，提供用户使用方法定义，非web用户权限相关不用写
		return array(
			/* 定义该类中文名称*/
			'class_name'=>array(__CLASS__,'转资记录'),
			'class_method' =>array(
				//array（方法名，中文解释，权值,是否为菜单）
				'show'  => array('show','列表',1,true),
				'add'  => array('add','添加',2,true),
				'edit'  => array('edit','编辑',4,false),
				'del'  => array('del','删除',8,false),
				'browse' => array('browse', '查看', 16, false),
				'show_p'=> array('show_p','',0,false)
			)
		);
	}
}

?>